
#include "hexfloat.h"
#include "atos_endian.h"

float be_hex_to_float(uint8_t *hex)
{
    hex_float_t data;

    if(hex == NULL)
        return 0.0;
#if defined(__LITTLE_ENDIAN__)
    data.hex[0] = hex[3];
    data.hex[1] = hex[2];
    data.hex[2] = hex[1];
    data.hex[3] = hex[0];
#elif defined (__BIG_ENDIAN__)
    data.hex[0] = hex[0];
    data.hex[1] = hex[1];
    data.hex[2] = hex[2];
    data.hex[3] = hex[3];
#endif
    return data.val;
}

float le_hex_to_float(uint8_t *hex)
{
    hex_float_t data;

    if(hex == NULL)
        return 0.0;
#if defined(__LITTLE_ENDIAN__)
    data.hex[0] = hex[0];
    data.hex[1] = hex[1];
    data.hex[2] = hex[2];
    data.hex[3] = hex[3];
#elif defined (__BIG_ENDIAN__)
    data.hex[0] = hex[3];
    data.hex[1] = hex[2];
    data.hex[2] = hex[1];
    data.hex[3] = hex[0];    
#endif
    return data.val;
}

int8_t float_to_be_hex(float val, uint8_t *out_hex)
{
    hex_float_t data;
    data.val = val;
    
    if(out_hex == NULL)
        return -1;
#if defined(__LITTLE_ENDIAN__)
    out_hex[0] = data.hex[3];
    out_hex[1] = data.hex[2]; 
    out_hex[2] = data.hex[1]; 
    out_hex[3] = data.hex[0];
#elif defined (__BIG_ENDIAN__)
    out_hex[0] = data.hex[0];
    out_hex[1] = data.hex[1]; 
    out_hex[2] = data.hex[2]; 
    out_hex[3] = data.hex[3];   
#endif
    return 0;
}

int8_t float_to_le_hex(float val, uint8_t *out_hex)
{
    hex_float_t data;
    data.val = val;

    if(out_hex == NULL)
        return -1;
#if defined(__LITTLE_ENDIAN__)
    out_hex[0] = data.hex[0];
    out_hex[1] = data.hex[1]; 
    out_hex[2] = data.hex[2]; 
    out_hex[3] = data.hex[3];
#elif defined (__BIG_ENDIAN__)
    out_hex[0] = data.hex[3];
    out_hex[1] = data.hex[2]; 
    out_hex[2] = data.hex[1]; 
    out_hex[3] = data.hex[0];   
#endif
    return 0;
}
